package com.yuanhang.readsms;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lxj.xpopup.XPopup;
import com.yuanhang.readsms.base.BaseRVAdapter;
import com.yuanhang.readsms.base.BaseRVHolder;
import com.yuanhang.readsms.bean.Phone;
import com.yuanhang.readsms.databinding.ActivityNumberManagementBinding;

import org.litepal.LitePal;
import org.w3c.dom.Text;

import java.util.List;

/**
 * 号码管理
 */
public class NumberManagementActivity extends AppCompatActivity implements View.OnClickListener {

    private ActivityNumberManagementBinding binding;
    private int AddCode = 201;
    private BaseRVAdapter<Phone> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNumberManagementBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
        initData();
    }

    private void initView() {
        binding.icTitle.tvTitle.setText("号码管理");
        binding.icTitle.ivBack.setOnClickListener(this);
        binding.cvAddNum.setOnClickListener(this);
    }

    private void initData() {
        //查询所有号码
        List<Phone> allPhone = LitePal.order("addPhoneNumTime desc").find(Phone.class);
        adapter = new BaseRVAdapter<Phone>(R.layout.item_phone_number) {
            @Override
            public void onBindVH(BaseRVHolder holder, Phone data, int position) {
                TextView tvDelete = holder.getView(R.id.tvDelete);

                holder.setText(R.id.tvNumber, data.getPhoneNum());
                holder.setText(R.id.tvAddTime, data.getAddPhoneNumTime());

                tvDelete.setOnClickListener(view -> {
                    //删除操作
                    new XPopup.Builder(NumberManagementActivity.this).asConfirm("提示",
                            "确定删除该数据吗",
                            () -> {
                                LitePal.delete(Phone.class, data.getId());
                                adapter.remove(position);
                            })
                            .show();
                });
            }
        };
        binding.rvPhoneNumber.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        binding.rvPhoneNumber.setAdapter(adapter);
        adapter.setNewData(allPhone);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 201 && resultCode == Activity.RESULT_OK) {
            List<Phone> allPhone = LitePal.order("addPhoneNumTime desc").find(Phone.class);
            adapter.setNewData(allPhone);
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivBack) {
            finish();
        } else if (id == R.id.cvAddNum) {
            //点击跳转到添加号码页面
            Intent intent = new Intent(NumberManagementActivity.this, AddNumberActivity.class);
            startActivityForResult(intent, AddCode);
        }
    }
}